package com.sdx.console.spread.web;

import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.web.AuthorizedBaesController;
import com.ibeetl.admin.core.web.JsonResult;
import com.sdx.console.rightRole.entity.RightRole;
import com.sdx.console.spread.entity.SpreadShare;
import com.sdx.console.spread.service.SpreadShareService;
import com.sdx.console.system.entity.ChannelSet;
import com.sdx.console.system.service.ChannelSetService;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName SpreadShareController
 * @Description 分享推广码
 * @Author kangbin
 * @Date 2020/3/20 15:59
 */
@RequestMapping("/spread/share")
@Controller
public class SpreadShareController extends AuthorizedBaesController {

    private static final String MODEL = "/spread/share";

    @Resource
    private SpreadShareService spreadShareService;

    @Resource
    private ChannelSetService channelSetService;

    @Resource
    private CorePlatformService corePlatformService;


    /**
     * 初始化页面需要的列表数据，
     *
     */
    private void setPageParam(ModelAndView view,HttpServletRequest request) {
        CoreUser user = getLoginUser(request);
        Integer accessType = corePlatformService.getAccessType(request);
        List<ChannelSet> channelList = channelSetService.selectAuthChannels(accessType, user.getOrgId(),user.getTenantId());
        view.addObject("channels",channelList);
    }

    /* 页面 */

    @GetMapping("/index.do")
    @Function("spread.share.query")
    @ResponseBody
    public ModelAndView index(HttpServletRequest request,String channelCode) {
        ModelAndView view = new ModelAndView(MODEL+"/index.html") ;
        setPageParam(view,request);
        view.addObject("channel",channelCode);
        return view;
    }

    @PostMapping("/list.json")
    @Function("spread.share.query")
    @ResponseBody
    public JsonResult<PageQuery> list(SpreadShare condtion, HttpServletRequest request,String channel)
    {
        CoreUser user = getLoginUser(request);
        if(StringUtils.isNotEmpty(channel)){
            condtion.setChannelCode(channel);
        }
        condtion.setTenantId(user.getTenantId());
        PageQuery page = condtion.getPageQuery();
        spreadShareService.queryByCondition(page);
        return JsonResult.success(page);
    }

}
